package com.link.base.base.applyquality.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.base.base.applyquality.model.ApplyQuality;
import com.link.base.base.applyquality.model.ApplyQualityMeg;
import com.link.base.base.applyquality.service.ApplyQualityService;
import com.link.base.base.approval.model.Flow;
import com.link.base.base.approval.service.FlowService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liushoulong
 * @date 2018-11-08
 * @desc 酒体质检报告申请
 */
@Controller("ApplyQualityController")
@RequestMapping("/link/applyQuality")
public class ApplyQualityController extends BasicController<ApplyQuality> {

    @Resource
    private ApplyQualityService applyQualityService;

    @Resource
    private FlowService flowService;

    @Override
    public BasicService<ApplyQuality> getBasicService() throws Exception {
        return applyQualityService;
    }

    /**
     * 提交申请，创建审批流程
     */
    @RequestMapping(value = "/submit")
    @ResponseBody
    public Map<String, Object> submit(@RequestBody ApplyQualityMeg applyQualityMeg,
                                      HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            String approvalDetail = mapper.writeValueAsString(applyQualityMeg);
            if (null == applyQualityMeg.getApplyQuality().getId()) {
                throw new ServiceException("PUBLIC-007");
            }
            applyQualityService.accntSigningSubmit(applyQualityMeg.getApplyQuality(), approvalDetail);
            ApplyQuality newTra = applyQualityService.queryById(applyQualityMeg.getApplyQuality().getId());
            result.put("newRow", newTra);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 审批处理 第一个节点填写数据 审批详情数据修改
     *
     * @param apptrovalInfo
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/approvalDetailUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> approvalDetailUpdate(@RequestBody ApplyQualityMeg apptrovalInfo, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            if (null == apptrovalInfo.getApprovalId()) {
                throw new ServiceException("APPLY-001");
            }
            Flow flow = new Flow();
            flow.setId(apptrovalInfo.getApprovalId());
            Flow flowObj = flowService.queryById(flow);
            if (flowObj == null) {
                throw new ServiceException("APPLY-002");
            }
            /*审批详情封装json字符串*/
            String approvalDetail = mapper.writeValueAsString(apptrovalInfo);
            /*审批流程更新*/
            flowObj.setFlowObjDetail(approvalDetail);
            flowService.update(flowObj);
            result.put("success", true);
            result.put("newRow", flowService.queryById(flowObj));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 审核处理 最后一个节点审核成功 修改审批对象
     *
     * @param approvalInfo
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/approvalObjectUpdate", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> approvalObjectUpdate(@RequestBody ApplyQualityMeg approvalInfo, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        ObjectMapper mapper = new ObjectMapper();
        try {
            applyQualityService.approvalObjectUpdate(approvalInfo);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }


    /**
     * 根据用户查询申请列表
     *
     * @return
     */
    @RequestMapping(value = "/queryByUserExamplePage")
    @ResponseBody
    public Map<String, Object> queryByUserExamplePage(@JsonParam ApplyQuality applyQuality,
                                                      @JsonParam QueryParams qps, HttpSession session) {
        Map<String, Object> result = new HashMap<String, Object>(8);
        try {
            List<ApplyQuality> applyQualityList = applyQualityService.queryByUserExamplePage();
            result.put("rows", applyQualityList);
            result.put("success", true);

        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
        }
        return result;
    }
}
